﻿@using Abp.Json
@using Partner.Surround.Notifications.Dto
@model GetNotificationSettingsOutput
@{
    var notificationNames = Model.Notifications.Select(n => n.Name);
}

@section styles{
    <style type="text/css">
        .layui-form-wd130 .layui-form-label {
            width: 130px;
        }

        .layui-form-wd130 .layui-input-block {
            margin-left: 140px;
        }
    </style>
}

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">
            <h5>通知设置</h5>
        </div>
        <div class="layui-card-body layui-form layui-row">
            <form class="layui-form layui-form-wd130" lay-filter="layui-form-notificationSettings">
                @foreach (var notification in Model.Notifications)
                {
                    <div class="layui-form-item">
                        <label class="layui-form-label">@notification.DisplayName</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="@notification.Name" name="@notification.Name" lay-skin="switch" lay-text="开|关" @(notification.IsSubscribed ? "checked=\"checked\"" : "") @(!Model.ReceiveNotifications ? "disabled" : "")>
                        </div>
                        @*<label class="layui-form-label">@notification.Description</label>*@
                    </div>
                }
                <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <button class="layui-btn" lay-submit lay-filter="notification_Form_Submit">提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

@section scripts{
    <script type="text/javascript">
        layui.config({
            base: '/lib/layui-extend-modules/' //静态资源所在路径
        }).use(['jquery', 'element', 'form', 'layer'], function () {
            var $ = layui.$,
                form = layui.form,
                element = layui.element;

            form.on('submit(notification_Form_Submit)', function (data) {
                var notifications = [];
                var notificationNamesArray = @Html.Raw(JsonExtensions.ToJsonString(notificationNames, true, false));
                for (var i = 0; i < notificationNamesArray.length; i++) {
                    var isSubscribed = data.field[notificationNamesArray[i]] == "on";//switch默认值是on
                    notifications.push({ "name": notificationNamesArray[i], "isSubscribed": isSubscribed});
                }

                var field = {
                    "receiveNotifications": true,
                    "notifications": notifications
                };

                var waitIndex = parent.layer.load(2);
                abp.ajax({
                    url: "@Url.Action("UpdateNotificationSetting", "Users")",
                    data: JSON.stringify(field),
                    abpHandleError: false
                }).done(function (data) {
                    if (data.code == 200) {
                        abp.notify.success(data.msg);
                    }
                }).fail(function (jqXHR) {
                    parent.layer.msg(jqXHR.message, { icon: 5 });
                }).always(function () {
                    parent.layer.close(waitIndex);
                });

                return false;
            });
        });
    </script>
}